package io.renren.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.entity.LaboratoryEntity;
import io.renren.entity.Result;
import io.renren.entity.SubscribeEntity;
import io.renren.service.SubscribeService;
import io.renren.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * describe:
 * create @ kylin  2019/03/04
 */
@RestController
@Api(tags = "实验室预约")
@RequestMapping(value = "api/subsc")
public class ApiSubscribeController {

    @Autowired
    private SubscribeService subscribeService;

    @ApiOperation(value = "获取到所有的院系")
    @GetMapping("/department/list")
    public Result<SubscribeEntity> getAllDepartment() throws Exception{
        return ResultUtil.success(this.subscribeService.getAllDepartment());
    }

    @ApiOperation(value = "获取到所有的空闲房间")
    @GetMapping("/room/list")
    public Result<SubscribeEntity> getAllRoom() throws Exception{

        return ResultUtil.success(this.subscribeService.getAllRoom());
    }

    @ApiOperation(value = "实验室预约申请")
            @PostMapping(value = "/save")
    public Result<SubscribeEntity> subs(@RequestParam("endTime_1") String endTime_1, @RequestParam("starTime_1") String starTime_1,
                                        String suName, String suDepartmentUuid,
                                        String suDepartment, String suRoomId,String subcontent,
                                        String suRoom,String openid) throws Exception{
        SubscribeEntity subscribeEntity = sertParam(endTime_1, starTime_1, suName, suDepartmentUuid, suDepartment, suRoomId, suRoom,openid,subcontent);
        boolean save = this.subscribeService.save(subscribeEntity);

        if (save){
            return ResultUtil.success("写入成功");
        }else {
            return ResultUtil.error(1,"写入失败");
        }
    }

    @ApiOperation(value = "实验预约申请")
    @PostMapping(value = "/insertOneSubsExp")
    public Result<SubscribeEntity> insertOneSubsExp(@RequestParam("endTime_1") String endTime_1, @RequestParam("starTime_1") String starTime_1,
                                        String suName, String suDepartmentUuid,String suDepartment, @RequestParam("suRoomId") String suRoomId,
                                        String suRoom, String subject, String expContent,String subcontent,
                                                    String openid) throws Exception{
        SubscribeEntity subscribeEntity = sertParam(endTime_1, starTime_1, suName, suDepartmentUuid, suDepartment, suRoomId, suRoom,openid,subcontent);

        int r = this.subscribeService.insertOneSubsExp(subscribeEntity);
        if (r==1){
            return ResultUtil.success("写入成功");
        }else {
            return ResultUtil.error(1,"写入失败");
        }
    }

    private SubscribeEntity sertParam(@RequestParam("endTime_1") String endTime_1, @RequestParam("starTime_1") String starTime_1, String suName,
                                      String suDepartmentUuid, String suDepartment,
                                      String suRoomId, String suRoom,String openid,
                                      String subcontent) throws ParseException {
        SubscribeEntity subscribeEntity = new SubscribeEntity();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        subscribeEntity.setSuName(suName);
        subscribeEntity.setSuDepartmentUuid(suDepartmentUuid);
        subscribeEntity.setSuRoom(suRoomId);
        subscribeEntity.setSuDepartment(suDepartment);
        subscribeEntity.setSuRoom(suRoom);
        subscribeEntity.setSuTime(new Date());
        subscribeEntity.setSubcontent(subcontent);
        subscribeEntity.setStartTime(sdf.parse(starTime_1));
        subscribeEntity.setEndTime(sdf.parse(endTime_1));
        subscribeEntity.setOpenid(openid);
        return subscribeEntity;
    }


    @ApiOperation(value = "获取某个学生的所有预约")
    @GetMapping(value = "my/{openid}")
    public Result<SubscribeEntity> mysubs(@PathVariable("openid") String openid) throws Exception{
        QueryWrapper<SubscribeEntity> queryWrapper = new QueryWrapper();
        return ResultUtil.success(this.subscribeService.list(queryWrapper.lambda().eq(SubscribeEntity::getOpenid,openid)));
    }

}
